package xyz.cerberu.cet.shiro.utils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import xyz.cerberu.cet.user.domain.User;

/**
 * @author β世界
 * Created on 8:13
 * @Description shiro工具类
 */
public class ShiroUtils {

    /**
     * @author β世界 on 8:13 2021/4/7
     * 刷新会话的用户信息
     * @param user: 当前的用户信息
     * @return void
     **/
    public static void flushUser(User user){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principalCollection = subject.getPrincipals();
        String realmName = principalCollection.getRealmNames().iterator().next();
        PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
        subject.runAs(newPrincipalCollection);
    }

}
